package Service;

import Pojo.Reader;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ReaderService {
    public static List<Reader> findReaderAll(String id, String name, String sex, String age, String sub){
        List<Reader>list = new ArrayList<>();
        try{
            BufferedReader br = new BufferedReader(new FileReader("D:\\Demo123\\src\\image\\Reader.txt"));
            String line = null;
            while ((line=br.readLine())!=null){
                Reader reader=new Reader();
                String[] array = line.split(",");
                reader.setId(array[0]);
                reader.setName(array[1]);
                reader.setSex(array[2]);
                reader.setAge(array[3]);
                reader.setSub(array[4]);
                list.add(reader);
            }
        }catch (FileNotFoundException e){
            throw new RuntimeException(e);
        }catch(IOException e){
            throw new RuntimeException(e);
        }
        if(id==null || name==null || sex==null||age==null||sub==null){
            return list;
        }else if(id!=null){
            List<Reader>Idlist = new ArrayList<>();
            for(int i=0;i<list.size();i++){
                if(list.get(i).getId().equals(Idlist)){
                    Idlist.add(list.get(i));
                }
            }
            return Idlist;
        }
        return list;
    }
    public String[][]listToArray(List<Reader>list){
        String[][]obj = new String[list.size()][3];
        for(int i= 0;i<list.size();i++){
            Reader reader = list.get(i);
            obj[i] = new String[]
                    {
                            reader.getId(),reader.getName(),reader.getSex(),reader.getAge(),reader.getSub()
                    };
        }
        return obj;
    }
    public static void delReader(int index) {
        List<Reader> list = findReaderAll(null,null,null,null,null);
        list.remove(index);
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\Demo123\\src\\image\\Reader.txt"));
            for (int i = 0; i < list.size(); i++) {
                String line = list.get(i).getId() + "," + list.get(i).getName() + "," + list.get(i).getSex() + "," + list.get(i).getAge()+","+list.get(i).getSub();
                bw.write(line);
                if (i != list.size() - 1) {
                    bw.newLine();
                }
            }
            bw.flush();
        }catch (IOException e){
            throw new RuntimeException(e);
        }

    }
    public static Reader findReaderById(int index){
        List<Reader>list = findReaderAll(null,null,null,null,null);
        return list.get(index);
    }
    public void editReader(Reader reader){
        List<Reader> list = findReaderAll(null,null,null,null,null);
        for(int i=0;i<list.size();i++){
            if(list.get(i).getId().equals(reader.getId())){
                list.set(i,reader);
            }
        }
        try{
            BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\Demo123\\src\\image\\Reader.txt"));
            for (int i=0;i<list.size();i++){
                Reader reader1 = list.get(i);
                String line = reader1.getId()+","+reader1.getName()+","+reader1.getSex()+","+reader1.getAge()+","+reader1.getSub();
                bw.write(line);
                if(i<list.size() -1){
                    bw.newLine();
                }
            }
            bw.flush();

        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }
    public static void saveReader( String name, String sex, String age,String sub){
        try {
            BufferedWriter bw = new BufferedWriter(new
                    FileWriter("D:\\Demo123\\src\\image\\Reader.txt",true));
            String line = name +","+sex+","+ age+","+sub;
            bw.newLine();
            bw.write(line);
            bw.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
